package org.piggy.test.http.controller;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
public class HttpDemoThirdController {
    @ResponseBody
    @GetMapping("/http-demo/pong")
    public String ping() {
        return "pong";
    }

    private ApplicationContext applicationContext;

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @ResponseBody
    @GetMapping("/http-demo/mappings")
    public Map<String, Object> getMappings() {
        Map<String, RequestMappingHandlerMapping> mappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RequestMappingHandlerMapping.class, true, false);

        Map<String, Object> result = new HashMap<>();

        Set<Map.Entry<String, RequestMappingHandlerMapping>> entries0 = mappings.entrySet();

        for(Map.Entry<String, RequestMappingHandlerMapping> entry0 : entries0){
            String key0 = entry0.getKey();
            RequestMappingHandlerMapping value0 = entry0.getValue();
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = value0.getHandlerMethods();
            Set<Map.Entry<RequestMappingInfo, HandlerMethod>> entries1 = handlerMethods.entrySet();
            for (Map.Entry<RequestMappingInfo, HandlerMethod> entry1 : entries1){
                RequestMappingInfo key1 = entry1.getKey();
                HandlerMethod value1 = entry1.getValue();
            }
        }

        return result;
    }

}
